import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class Main {
    // 储存账户信息
    private static Map<String,Bank> accounts;
    private static Scanner scanner = new Scanner(System.in);
    private static Bank bankAccount;

    //初始化账户信息
    public static boolean init() {
        Bank account1 = new Bank("张三", "11234", "123", 1000.99);
        Bank account2 = new Bank("李四", "22134", "456", 2000);
        Bank account3 = new Bank("王五", "33124", "789", 100.99);
        accounts = new HashMap<String, Bank>();
        accounts.put(account1.getBankId(), account1);
        accounts.put(account2.getBankId(), account2);
        accounts.put(account3.getBankId(), account3);
        return accounts.isEmpty() ? false : true;                    // 返回初始化结果
    }

    //系统入口
    public static void main(String[] args) {
        System.out.println("银行系统正在启动...");
        if (init()) {
            System.out.println("欢迎进入网上银行系统！");
            System.out.println("输入卡号：");
            String id = scanner.next();
            System.out.println("输入密码：");
            String psw = scanner.next();
            if (login(id, psw)) {
                System.out.println("登录成功，欢迎使用！");               // 进入系统
                menu(bankAccount);
            } else {
                System.out.println("登录失败，退出系统");
            }
        } else {
            System.out.println("系统启动失败");
        }
    }

    //返回登录结果
    private static boolean login(String id, String psw) {
        bankAccount = accounts.get(id);
        return null == bankAccount ? false : true;
    }

    //功能菜单
    private static void menu(Bank account) {
        System.out.println("当前账户：" + account.getName());
        main: while (true) {
            System.out.println("1:存款 2:取款 3:余额 0:退出");
            int item = scanner.nextInt();
            switch (item) {
                case 0:
                    scanner.close();
                    System.out.println("退出系统，感谢使用！");
                    break main;
                case 1:
                    System.out.println("--存款系统--");
                    saveMoney(accounts, account);
                    break;
                case 2:
                    System.out.println("--取款系统--");
                    ereduceMoney(accounts, account);
                    break;
                case 3:
                    System.out.println("--余额系统--");
                    showMoney(account);
                    break;
                case 4:
                    System.out.println("--修改密码--");
                    //rePassword(accounts, account);
                    break;
                default:
                    System.out.println("--错误的指令--");
                    break;
            }
        }
    }

    private static void showMoney(Bank account) {
        System.out.println(account);
    }

    //取款功能
    private static void ereduceMoney(Map<String, Bank> accounts2, Bank account) {
        System.out.println("输入取款金额：");
        double money = scanner.nextDouble();
        double now = account.getMoney();
        if (money <= now) {
            account.setMoney(now - money);
            accounts2.put(account.getBankId(), account);
            System.out.println("恭喜您，取款成功！");
        } else {
            System.out.println("余额不足，取款失败！");
        }
    }

    //存款功能
    private static void saveMoney(Map<String, Bank> accounts2, Bank account) {
        System.out.println("输入存入金额：");
        double money = scanner.nextDouble();
        if (money<0){
            System.out.println("非正确金额，回到主界面");
            return;
        }
        account.setMoney(account.getMoney() + money);
        accounts2.put(account.getBankId(), account);
        System.out.println("恭喜您，存款成功，谢谢使用");
    }
}